$(function() {
	app = new Vue({
		el : '#app',
		data : {
		},
		methods : {
			closeWin:function(){
			var index = parent.layer.getFrameIndex(window.name);
			parent.layer.close(index);
		},
		 resetForm:function(){
			$('#commdity-form')[0].reset();
		},

		 getCommodityId:function(){
			var addr = location.href;
			var index = addr.lastIndexOf('/') + 1;
			var id = addr.substr(index);
			return id;
		},

		 getCommodity:function(){
			 var ue = UE.getEditor('container');
			var id = this.getCommodityId();
			$.ajax({
			    type: "GET",
			    url: '/manager/commodity/get/'+id,
			    beforeSend: function () {
			    },
			    complete: function () {
			    },
			    error: function(request) {
			        alert("Connection error");
			    },
			    success: function(result) {
			        if (result.status == 0) {
			        	var data = result.data;
			        	loadUpdate(data.showImgs);
			        	new Vue({
			        		  el: '#commodity-form',
			        		  data: data
			        		});
			        	
			        	/*if(data.integralShopClassify != "") {
			        		var optHtml = "";
			        		for(var i = 0; i < data.integralShopClassify.length; i++) {
			        			optHtml += "<option value='"+data.integralShopClassify[i].id+"'>"+data.integralShopClassify[i].classifyName+"</option>";
			        		}
			        		console.log(optHtml);
			        		$("#shopclassifyName").html(optHtml);
			        	}*/
			        	if(data.shopclassifyName != "") {
			        		var shopclassifyName = data.shopclassifyName.split(",");
			        		var optHtml = "";
			        		for(var i = 0; i < shopclassifyName.length; i++) {
			        			optHtml += "<option>"+shopclassifyName[i]+"</option>";
			        		}
			        		$("#shopclassifyName").html(optHtml);
			        	}
			        	ue.ready(function(){
			        		ue.setContent(data.description);
			        	});
			        	
			        }
			    }
			});
		},

		init:function(){
			
			this.getCommodity();
			$("#commodity-form").validate({
				rules:{
					name : {
						required : true
					},
					integralPrice : {
						required : true
					},
					marketPrice : {
						required : true
					},
					status : {
						required : true
					},
					showImgs: {
						required : true
					},
					shopclassifyId: {
						required : true
					}
				},
				onkeyup:false,
				focusCleanup:true,
				success:"valid",
				submitHandler:function(form){
					var data = $('#commodity-form').serialize();
					/*var selectedclassifyIds = $("#shopclassifyName").find("option");
					for(var j = 0; j < selectedclassifyIds.length; j++) {
						data += "&integralShopClassify["+j+"].id=" + $(selectedclassifyIds[j]).prop("value");
					}	
					console.log($("#shopclassifyName").find("option"));*/
					if ($("#shopclassifyIds").val() != "") {
						var shopclassifyIds = $("#shopclassifyIds").val().split(",");
						for(var i = 0; i < shopclassifyIds.length; i++) {
							data += "&integralShopClassify["+i+"].id=" + shopclassifyIds[i];
						}
					}

					$.ajax({
					    type: "POST",
					    url: '/manager/commodity/update',
					    data: data,
					    beforeSend: function () {
					    	
					    },
					    complete: function () {
					    	
					    },
					    error: function(request) {
					    	layer.alert("系统出错");
					    },
					    success: function(data) {
					    	if (data.status == 0) {
					    		refreshParentList();
								layer.alert(data.message, function() {
									var index = parent.layer.getFrameIndex(window.name);
									parent.layer.close(index);
								});
							}else{
								layer.alert(data.message);
							}
					    }
					});
				}
			});
		}}
	});
	
	app.init();
});